/* privateChat.js */
import EmojiDecoder from '../../static/lib/EmojiDecoder'
let emojiUrl = 'https://imgcache.qq.com/open/qcloud/tim/assets/emoji/'
import emojiMap from '../../static/lib/emojiMap'
const util= require('../../utils/util.js')

const app = getApp()
const db = wx.cloud.database({
    env: 'work1-0g1b3grp436e9236'
})

Page({
	data: {
		content: '',
		friend: {},
		currentUser: {},
		messages: [],
		//所有历史消息加载完成标识
		allHistoryLoaded: false,
		//定义表情列表
		emoji : {
			url : emojiUrl,
			map : emojiMap,
			visible: false,
			decoder: new EmojiDecoder(emojiUrl, emojiMap),
		},
		//是否展示其他消息类型面板
		otherTypesMessagePanelVisible: false,
		// 展示消息删除弹出框
		actionPopup:{
			visible: false,
			message: null,
			recallable: false
		},
		// 消息选择
		messageSelector: {
			visible: false,
			messages: []
        },
	},
	onPullDownRefresh () {
		this.loadHistoryMessage(false);
	},
	onLoad: function(options) {
		// 获取初始数据并加载
        let friendId = options.to
        this.setData({friendId: friendId})
        // console.log(friendId)
        let currentUser = wx.getStorageSync('userInfo')
        let friend = {}
		db.collection('work_user').where({openid: friendId}).get().then(res=>{
            console.log('res', res)
            friend = res.data[0]
            this.setData({
                friend: friend,
                currentUser: currentUser
            })
            // 开启监听和加载历史数据
            this.initialGoEasyListeners()
            this.loadHistoryMessage(true)
        })
    },
    onShow() {
        this.scrollToBottom()
    },
	onUnload () {
		//退出聊天页面之前，清空监听器
		wx.goEasy.im.on(wx.GoEasy.IM_EVENT.MESSAGE_READ, () => {});
		wx.goEasy.im.on(wx.GoEasy.IM_EVENT.PRIVATE_MESSAGE_RECEIVED, ()=>{});
        wx.goEasy.im.on(wx.GoEasy.IM_EVENT.MESSAGE_DELETED, ()=>{});
        wx.goEasy.im.on(wx.GoEasy.IM_EVENT.MESSAGE_RECALLED, ()=>{});
	},
	initialGoEasyListeners() {
		// 监听私聊消息
		wx.goEasy.im.on(wx.GoEasy.IM_EVENT.PRIVATE_MESSAGE_RECEIVED, (message) => {
			console.log('PRIVATE_MESSAGE_RECEIVED:', message);
			let senderId = message.senderId;
			let receiverId = message.receiverId;
			let friendId = this.data.currentUser.userOpenId === senderId?receiverId:senderId;
			if (friendId === this.data.friend.openid) {
				this.data.messages.push(message);
				this.renderMessages(this.data.messages);
				//聊天时，收到消息标记为已读
				this.markPrivateMessageAsRead();
				//收到新消息，是滚动到最底部
				this.scrollToBottom();
			}
		});
        //监听消息删除
		wx.goEasy.im.on(wx.GoEasy.IM_EVENT.MESSAGE_DELETED,(deletedMessages) => {
            let needRender = false;
			deletedMessages.forEach(message => {
				let senderId = message.senderId;
				let receiverId = message.receiverId;
				let friendId = this.data.currentUser.userOpenId === senderId?receiverId:senderId;
				if (friendId === this.data.friend.openid) {
                    needRender = true;
					let index = this.data.messages.indexOf(message);
					if (index > -1) {
						this.data.messages.splice(index, 1);
                    }
				}
            });
            needRender && this.renderMessages(this.data.messages);
		})
		// 监听消息已读
		wx.goEasy.im.on(wx.GoEasy.IM_EVENT.MESSAGE_READ,(readMessages) => {
            let needRender = false;
			readMessages.forEach(message => {
				if (message.receiverId === this.data.friend.openid) {
					needRender = true;
				}
            })
            needRender && this.renderMessages(this.data.messages);
        })
        // 监听消息撤回
		wx.goEasy.im.on(wx.GoEasy.IM_EVENT.MESSAGE_RECALLED,(recalledMessages) => {
            this.renderMessages(this.data.messages);
        })
	},
	showActionPopup(e) {
		const selectedMessageId = e.currentTarget.dataset.messageid;
		let selectedMessage;
		this.data.messages.forEach(message => {
			if(message.messageId === selectedMessageId){
				selectedMessage = message;
			}
		});
		const MAX_RECALLABLE_TIME = 3 * 60 * 1000; //3分钟以内的消息才可以撤回
        let recallable = false;
		if ((Date.now() - selectedMessage.timestamp) < MAX_RECALLABLE_TIME && selectedMessage.senderId === this.data.currentUser.userOpenId && selectedMessage.status === 'success') {
			recallable = true;
		}
		this.setData({
			['actionPopup.recallable']: recallable,
            ['messageSelector.messages']: [selectedMessage],
			['actionPopup.visible']: true,
        });
	},
	hideActionPopup () {
		this.setData({
			['actionPopup.recallable']: false,
			['actionPopup.visible']: false,
			['messageSelector.messages']: [],
		});
	},
	deleteSingleMessage(){
		wx.showModal({
			content: '确认删除？',
			success: (res) => {
				this.setData({
					['actionPopup.visible']: false,
				});
				if (res.confirm) {
					this.deleteMessage();
				}
			},
		});
	},
	deleteMessage() {
		wx.goEasy.im.deleteMessage({
			messages: this.data.messageSelector.messages,
			onSuccess: ()=>{
				this.data.messageSelector.messages.forEach(message => {
					let index = this.data.messages.indexOf(message);
					if (index > -1) {
						this.data.messages.splice(index, 1);
					}
                });
				this.renderMessages(this.data.messages);
				this.setData({
					['messageSelector.messages']: [],
					['messageSelector.visible']: false,
				});
			},
			onFailed: (error) => {
				console.log('error:', error);
			}
		});
    },
    recallMessage() {
        this.setData({
            ['actionPopup.visible']: false,
        });
        wx.goEasy.im.recallMessage({
            messages: this.data.messageSelector.messages,
            onSuccess: ()=>{
                console.log('撤回成功');
                this.renderMessages(this.data.messages);
            },
            onFailed: (error) => {
                console.log('撤回失败,error:', error);
            }
        });
    },
    editRecalledMessage (e) {
	    if (this.data.recordVisible) {
		    this.setData({
			    ['recordVisible']: false,
		    });
	    }
        this.setData({
            ['content']: e.currentTarget.dataset.content,
        });
    },
	showCheckBox () {
		this.setData({
			['messageSelector.messages']: [],
			['messageSelector.visible']: true,
			['actionPopup.visible']: false,
        });
        this.data.messages.forEach(message => {
            message.checked = false;
        })
        this.renderMessages(this.data.messages);
	},
	selectMessages (e) {
		const selectedMessageIds = e.detail.value;
		let selectedMessages = [];
		this.data.messages.forEach(message => {
			if(selectedMessageIds.includes(message.messageId)){
				selectedMessages.push(message);
                message.checked = true;
			}
		})
		this.setData({
			['messageSelector.messages']: selectedMessages
		});
    },
    // 创建消息
	createTextMessage() {
        if (!wx.getStorageSync('userInfo')) {
            util.myToastNone('请先登录～')
            wx.switchTab({
              url: '/pages/center/center',
            })
            return
        }
        // console.log(this.data.content, this.data.friend.openid, this.data.friend.nickName, this.data.friend.avatar)
		// 发送文本与表情
		if (this.data.content.trim() !== '') {
            util.playBeep()
			let textMessage = wx.goEasy.im.createTextMessage({
				text: this.data.content,
				to: {
					id: this.data.friend.openid,
					type: wx.GoEasy.IM_SCENE.PRIVATE,
					data: {
						name: this.data.friend.nickName,
						avatar: this.data.friend.avatar
					}
				}
			})
			this.sendMessage(textMessage)
		}
		this.setData({
			content: ''
		})
    },
    // 发送消息
	sendMessage(message){
		let self = this
		let messages = this.data.messages
		messages.push(message)
		this.renderMessages(messages)
		this.scrollToBottom()
		wx.goEasy.im.sendMessage({
			message: message,
			onSuccess: function (message) {
				console.log('发送成功.', message);
				self.renderMessages(self.data.messages);
			},
			onFailed: function (error) {
				if(error.code === 507){
					console.log('发送语音/图片/视频/文件失败，没有配置OSS存储，详情参考：https://www.goeasy.io/cn/docs/goeasy-2.x/im/message/media/send-media-message.html');
				}else{
					console.log('发送失败:', error);
				}
				self.renderMessages(self.data.messages);
			}
        });
        this.setData({
			otherTypesMessagePanelVisible: false,
			['emoji.visible']: false
		});
    },
    //历史消息
	loadHistoryMessage(scrollToBottom) {
		let self = this
		let friendId = this.data.friend.openid
		let lastMessageTimeStamp
		let lastMessage = this.data.messages[0]
		if (lastMessage) {
			lastMessageTimeStamp = lastMessage.timestamp
        } else {
            lastMessageTimeStamp = null
        }
        console.log(friendId, lastMessageTimeStamp)
		wx.goEasy.im.history({
			userId: friendId,
			lastTimestamp: lastMessageTimeStamp,
			onSuccess: function (result) {
				wx.stopPullDownRefresh();
				let messages = result.content;
				if (messages.length === 0) {
					self.setData({
						allHistoryLoaded: true
					});
				} else {
					let messageList = messages.concat(self.data.messages);
					self.renderMessages(messageList);
					if (scrollToBottom) {
						self.scrollToBottom();
						//收到的消息设置为已读
						self.markPrivateMessageAsRead();
					}
				}
			},
			onFailed: function (error) {
				//获取失败
				console.log('获取历史消息失败, code:' + error.code + ',错误信息:' + error.content);
				wx.stopPullDownRefresh();
			}
		});
    },
    // 格式化消息列表
    renderMessages(messages){
        messages.forEach((message,index)=>{
            if(index === 0){
				// 当页面只有一条消息时，显示发送时间
                message.showTime = app.formatDate(message.timestamp);
            }else {
				// 当前消息与上条消息的发送时间进行比对，超过5分钟则显示当前消息的发送时间
				if (message.timestamp - messages[index - 1].timestamp > 5 * 60 * 1000) {
					message.showTime = app.formatDate(message.timestamp);
				}
            }
            if(message.type === 'text'){
				// 渲染表情与文本消息
                let text = this.data.emoji.decoder.decode(message.payload.text);
                message.node= text;
                message.editable = message.type === 'text' && Date.now() - message.timestamp < 60 * 1000;
            }
            if(message.type === 'file'){
				// 渲染文件消息
                message.size = (message.payload.size / 1024).toFixed(2);
			}
        });
        this.setData({
            messages: messages
        });
    },
	markPrivateMessageAsRead () {
		wx.goEasy.im.markPrivateMessageAsRead({
			userId: this.data.friend.openid,
			onSuccess: function () {
				console.log('标记为已读成功')
			},
			onFailed: function (error) {
				console.log(error);
			}
		});
	},
	setContent(e) {
		// 监听输入的消息
		let content = e.detail.value;
		this.setData({
			content: content
		});
	},
	previewImage(event) {
		// 预览图片
		let imagesUrl = [event.currentTarget.dataset.src];
		wx.previewImage({
			urls: imagesUrl // 需要预览的图片http链接列表
		});
	},
    selectEmoji(e){
		// 选择表情
	    let emojiKey = e.currentTarget.dataset.emojikey;
		emojiKey = this.data.content + emojiKey;
	    this.setData({
            content: emojiKey
        });
    },
	messageInputFocusin(){
		this.setData({
			otherTypesMessagePanelVisible: false,
			['emoji.visible']: false
		});
	},
	showEmoji(){
		this.setData({
			['emoji.visible']: true,
			otherTypesMessagePanelVisible: false,
			recordVisible: false
		});
		// 关闭手机键盘
		wx.hideKeyboard().then(console.log).catch(console.log);
	},
	showMore(){
		this.setData({
			otherTypesMessagePanelVisible: !this.data.otherTypesMessagePanelVisible,
			['emoji.visible']: false
		});
		// 关闭手机键盘
		wx.hideKeyboard().then(console.log).catch(console.log);
	},
    scrollToBottom() { // 滑动到最底部
        setTimeout(() => {
            wx.pageScrollTo({
                scrollTop : 200000,
                duration :10
            });
        },600)
    },
    downLoadFile (e) {
        const message = e.currentTarget.dataset.message;
        if (message.status !== 'success') {
			return
        }
	    const file = message.payload;
        wx.showModal({
            title: "点击下载文件",
            showCancel: true,
            success: (res) => {
                if (res.confirm) {
                    wx.showLoading({
                        title: "正在下载文件...",
                        mask: true
                    });
                    const downloadTask = wx.downloadFile({
                        url: file.url,
                        success: (res) => {
                            if (res.statusCode === 200) {
                                wx.hideLoading();
                                const filePath = res.tempFilePath
                                wx.openDocument({
                                    filePath: filePath,
                                    success: function(res) {
                                    },
                                    fail(error) {
                                        wx.showToast({
                                            title: "打开文档失败",
                                            icon: "none",
                                            duration: 3000
                                        });
                                    }
                                })
                            }
                        },
                        fail: (error) => {
                            wx.showToast({
                                title: "下载文件失败",
                                icon: "none"
                            });
                            console.log('error',error);
                        }
                    });
                    downloadTask.onProgressUpdate((res) => {
                        console.log('下载进度' + res.progress);
                    });
                }
            }
        });
    }
})
